<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \App\Discussion;
use App\Http\Requests\PostRequest;
use App\Markdown\Parser;

class PostController extends Controller
{
  public function __construct(){
  	$this->middleware('auth',['only'=>['create','createPost']]);
  }

  public function index(){
    $discussions = Discussion::paginate(15);
    return view('forum.index',compact('discussions'));
  }

  public function show($id){
    $discussion = Discussion::find($id);
    $parser  = new Parser();
    $content = $parser->makeHtml($discussion->content);
    return view('forum.show',compact('discussion','content'));
  }

  public function create(){
  	return view('forum.create');
  }

  public function createPost(PostRequest $request){
  	$userId = \Auth::user()->id;
  	$id = ['user_id' => $userId];
  	Discussion::create(array_merge($request->all(), $id));
  	return \Redirect::back();
  }
}
